package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread_4 implements Runnable {
    private int ticket = 10;
    @Override
    public void run()  {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+
                    "剩余" + this.ticket-- + "张票");
        }
    }
}
class MyThread_5 implements Callable<String> {
    private int ticket = 10;
    @Override
    public String call() throws Exception {
        for(int i = 0 ;i < 10000 ;i++) {
            System.out.println(Thread.currentThread().getName()+ticket--);
        }
        return "------------";
    }
}
public class test {
    public static void main(String[] args) throws Exception {
        Callable callable = new MyThread_5();
        FutureTask<String> ft = new FutureTask<>(callable);
        //只会有第一个启动的线程执行程序，直到callable对象方法执行结束。
        // FurtureTask会有阻塞锁的情况产生
        Thread mt1 = new Thread(ft,"A");
        Thread mt2 = new Thread(ft,"B");
        Thread mt3 = new Thread(ft,"C");
        mt1.start();
        mt2.start();
        mt3.start();


        System.out.println( ft.get());
    }
}

//public class test {
//    public static void main(String[] args) {
//        MyThread_4 mt = new MyThread_4();
//        Thread t1 = new Thread(mt,"A");
//        Thread t2 = new Thread(mt,"B");
//        Thread t3 = new Thread(mt,"C");
//        t1.setName("haha");
//        t1.start();
//        t2.start();
//        t3.start();
//        t1.run();
//
//    }
//}


//
//public class test {
//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        Callable<String> callable = new MyThread_5();
//        FutureTask<String> futureTask = new FutureTask<>(callable);
//        Thread thread = new Thread(futureTask,"A");
//        Thread thread1 = new Thread(futureTask,"Z");
//        thread.start();
//        thread1.start();
//        System.out.println(futureTask.get());
//    }
//}

//synchronized  的同步代码块   ，   同步方法小例


class MyThread_3 implements Runnable {
    private int ticket = 10000;
    @Override
    public void run() {

            while (ticket > 0)
                synchronized (this) {
                if(ticket > 0)
                    System.out.println(Thread.currentThread().getName() +
                            "剩余" + ticket--);
                }
    }
//        public void run () {
//            while (ticket > 0)
//                this.fun();
//        }
//        public synchronized void fun () {
//            if (ticket > 0)
//                System.out.println(Thread.currentThread().getName() +
//                        "剩余" + ticket--);
//    }
}
//public class test {
//    public static void main(String[] args) {
//        MyThread_3 mt = new MyThread_3();
//        Thread mt1 = new Thread(mt,"A");
//        Thread mt2 = new Thread(mt,"B");
//        Thread mt3 = new Thread(mt,"C");
//        mt1.start();
//        mt2.start();
//        mt3.start();
//
//    }
//}








class MyThread_2 implements Callable {
    private int ticket = 10;

    @Override
    public String  call() throws Exception {
        while(this.ticket > 0) {
            System.out.println(Thread.currentThread().getName()+"剩余票数"+this.ticket--);
        }
        return "售罄";
    }
}
//public class test {
//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        FutureTask<String> task = new FutureTask<>(new MyThread_2());
//        new Thread(task,"A").start();
//        new Thread(task,"B").start();
//        System.out.println(task.get());
//    }
//}







class MyThread_1 implements Runnable {
    private String title =  "A";
    private int ticket = 1000;
//    public MyThread_1(String title) {
//        this.title = title;
//    }
    @Override
    public void run() {
       while(ticket-- > 0) {
            System.out.println(Thread.currentThread().getName()+"还剩"+ticket+"张票");
        }
    }
}
//覆写一个黄牛卖票
//输出新线程的名称
//public class test {
//    public static void main(String[] args) {
////       MyThread_1 mt1 = new MyThread_1();
////        new Thread(mt1).start();
////        new Thread(mt1).start();
//
//        MyThread_1 mt = new MyThread_1() ;
//        new Thread(mt,"lala").start();
//        new Thread(mt,"haha").start();
//
//    }
//}





//public class test {
//    public static void main(String[] args) {
//        MyThread_1 mt1 = new MyThread_1("线程A");
//        MyThread_1 mt2 = new MyThread_1("线程B");
//        MyThread_1 mt3 = new MyThread_1("线程C");
//        //核心：创建匿名Thread类对象来调用它的start方法
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("Hello World");
//            }
//        }).start();
//        //实现了接口Runnable ，但是Thread 类的start方法没有被继承
//        //所以需要创建一个匿名的Thread对象，来调用start方法启动多线程
//        //对于Runnable接口对象，此时也可以用匿名内部类或lambda表达式定义
//
//        //匿名内部类
//
////        new Thread(new Runnable() {
////            @Override
////            public void run() {
////                System.out.println("Hello World");
////            }
////        }).start();
//
//        //lambda表达式
////        Runnable runnable = () -> System.out.println("Hello World");
////        new Thread(runnable).start();
//
//
//
//    }
//}

//1.直接继承Thread类开启多线程

class MyThread extends Thread { //线程主类
    private String  title;
    public MyThread (String title) {
        this.title = title;
    }
    public void run() {
        for(int i = 0;i < 3;i++) {
            System.out.println(this.title +"\t"+ i);
        }
    }
}

//public class test {
//    public static void main(String[] args) {
//        MyThread mt1 = new MyThread("A线程");
//        MyThread mt2 = new MyThread("B线程");
//        MyThread mt3 = new MyThread("C线程");
//
//        mt1.start();
//        mt2.start();
//        mt3.start();
//    }
//}
